﻿#region

using System;
using DomainModel.Domain.Model;
using DomainModel.Domain.Model.ServiceModel;
using EsbMonitor.Ninject;
using EsbMonitorMessages.Events;
using NServiceBus;

#endregion

namespace EsbMonitor.Handlers
{
    public class ServiceAliveEventHandler : IHandleMessages<IServiceAlive>
    {
        public IBus Bus { get; set; }

        public void Handle(IServiceAlive message)
        {
            var servicesRepository = KernelContainer.Get<IServicesRepository>();

            QueueInfo queueInfo = new QueueInfo(message.ServiceInfo.InputQueue.QueueName);

            Service service = servicesRepository.Get(queueInfo);

            if (service == null)
            {
                Bus.HandleCurrentMessageLater();
                return;
            }

            service.LastAlive = message.EventTime;
            service.CreateModifyInfo.LastModified = DateTime.Now;

            servicesRepository.Store(service);
        }
    }

  
}